#!/usr/bin/env python
# -*- coding:utf-8 -*-
import datetime

from conn.MyLogs import MyLogs


class MailLog:
    id = 0
    sno = ''
    rev_mail = ''
    send_time = ''
    status = 0
    info = ''
    attach_file_name = ''
    crfrom = ''
    title = ''

    def __init__(self, sno, rev_mail, status, info, attach_file_name, crfrom, title):
        self.id = 0
        self.sno = sno
        self.rev_mail = rev_mail
        self.send_time = datetime.datetime.now().strftime("%Y-%m-%d %X")
        self.status = status
        self.info = info
        self.attach_file_name = attach_file_name
        self.crfrom = crfrom
        self.title = title

    @staticmethod
    def load_cols():
        c_cols = []
        c_cols.append('`id`')
        c_cols.append('`sno`')
        c_cols.append('`rev_mail`')
        c_cols.append('`send_time`')
        c_cols.append('`info`')
        c_cols.append('`status`')
        c_cols.append('`attach_file_name`')
        return ','.join(str(i) for i in c_cols)

    @staticmethod
    def split_params(params):
        g_param = []
        if len(params.sno) > 0:
            g_param.append('`sno`=' + "\'" + params.sno + "\'")
        if len(params.rev_mail) > 0:
            g_param.append('`rev_mail`=' + "\'" + params.rev_mail + "\'")
        if params.status > 0:
            g_param.append('`status`=' + "\'" + params.status + "\'")
        if len(params.info) > 0:
            g_param.append('`info`=' + "\'" + params.info + "\'")
        if len(params.send_time) > 0:
            g_param.append('`send_time`=' + "\'" + params.send_time + "\'")
        if len(params.attach_file_name) > 0:
            g_param.append('`attach_file_name`=' + "\'" + params.attach_file_name + "\'")
        return ','.join(str(i) for i in g_param)

    @staticmethod
    def split_params_with_and(params):
        g_param = []
        if len(params.sno) > 0:
            g_param.append('`sno`=' + "\'" + params.sno + "\'")
        if len(params.rev_mail) > 0:
            g_param.append('`rev_mail`=' + "\'" + params.rev_mail + "\'")
        if params.status > 0:
            g_param.append('`status`=' + "\'" + params.status + "\'")
        if len(params.info) > 0:
            g_param.append('`info`=' + "\'" + params.info + "\'")
        # if len(params.send_time) > 0:
        #     g_param.append('`send_time`=' + "\'" + params.send_time + "\'")
        if len(params.attach_file_name) > 0:
            g_param.append('`attach_file_name`=' + "\'" + params.attach_file_name + "\'")
        if len(params.crfrom) > 0:
            g_param.append('`crfrom`=' + "\'" + params.crfrom + "\'")
        return ' AND '.join(str(i) for i in g_param)


class MailLogService:

    @staticmethod
    def info(id):
        try:
            return {'code': 200, 'data': None, 'msg': ''}
        except Exception as e:
            MyLogs.logger.error("ERROR: %s" % e)
            return {'code': 500, 'data': '', 'msg': ''}

    @staticmethod
    def fetch_log(rev_mail, crfrom):
        try:

            return {'code': 200, 'data': None, 'msg': ''}
        except Exception as e:
            MyLogs.logger.error("ERROR: %s" % e)
            return {'code': 500, 'data': '', 'msg': ''}

    @staticmethod
    def add_log(mail_log):
        try:
            return {'code': 200, 'data': None, 'msg': ''}
        except Exception as e:
            MyLogs.logger.error("ERROR: %s" % e)
            return 500

    @staticmethod
    def chg_log_status(mail_sno, status):
        try:
            return {'code': 200, 'data': None, 'msg': ''}
        except Exception as e:
            MyLogs.logger.error("ERROR: %s" % e)
            return 500
